/**
 * 
 */
package com.yidu.view;

import java.util.List;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.bean.TbType;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yidu.commom.Tool;
import com.yidu.message.Message;
import com.yidu.message.Pages;
import com.yidu.service.TbTypeService;

/**
 * 类型表的实现类
 * @author likon
 * @date 2018年7月17日 上午11:34:27
 * @version 1.0
 */
@Controller
@RequestMapping("/type")
public class TbTypeAction {
	
	//打印控制台
	Logger logger = Logger.getLogger(TbMemberAction.class);

	//消息信息类
	Message msg = new Message();

	//json
	ObjectMapper mapper = new ObjectMapper();

	//店家类型
	@Resource
	private TbTypeService typeService;

	@RequestMapping("/show")
	public String showType(){
		logger.info("进入显示界面...");
		return "background/type/type";
	}

	/**
	 * 查询所有类型
	 * @param example
	 * @return
	 */
	@RequestMapping("/findType")
	@ResponseBody
	public List<TbType> findType(TbType example){

		Pages page = new Pages();
		page.setPage(1);
		page.setLimit(100);
		
		List<TbType> list = typeService.findVague(page ,example);
		logger.info("findType"+list.size());
		return list;
	}
	/**
	 * 自行判断 增加 修改<br>
	 * 根据ID是否存在
	 * @param member
	 * @return
	 * @throws JsonProcessingException 
	 * @throws IOException 
	 */
	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	public Object saveOrUpdate(HttpServletRequest request,TbType type) throws JsonProcessingException {

		logger.info("进入增加修改方法...");

		logger.info(mapper.writeValueAsString(type));
		
		int num = 0;
		//判断是否存在ID
		if(!Tool.isEmpty(type.getTypeId())){
			//存在 则修改
			num  = typeService.update(type);
		}else{
			//反之 增加
			type.setTypeId(Tool.getRandomString(32));
			num = typeService.save(type);
		}
 
		//根据 操作返回
		msg.setNum(num);
		if(num>0){
			msg.setMsg("操作成功!");
		}else{
			msg.setMsg("操作失败!");
		}
		return msg;
	}

	/**
	 *  接受前台 传递过来的参数<br>
	 *  执行删除方法
	 * @param typeId
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public  Object delete(String typeId){


		logger.info("进入删除方法....");
		int row = typeService.delete(typeId);
		//HttpServletResponse response = ServletActionContext.getResponse();
		//response.setCharacterEncoding("utf-8");
		if(row>0){
			msg.setMsg("删除成功!");
		}else{ 
			msg.setMsg("删除失败!");
		} 
		return msg;
	}


	@RequestMapping("/findVague")
	@ResponseBody
	public Object findVague(HttpServletRequest request,Pages pages,TbType type) throws IOException{
		logger.info("进入模糊查询...");


		logger.info(mapper.writeValueAsString(pages));
		//得到查询结果集
		List<TbType> list = typeService.findVague(pages, type);

		 
		logger.info(mapper.writeValueAsString(list));

		//类型转换
		Long lon= (long) typeService.findCount(type);
		int row = lon.intValue();


		//由于前端所需条件
		Map<String, Object> map = new HashMap<>();

		map.put("code", 0);
		map.put("msg","");
		map.put("count",row);
		map.put("data", list);
		return map;
	}

	/**
	 * 根据ID查询对象类型
	 * 
	 * @param id
	 * @param response
	 * @return
	 */
	@RequestMapping("/showUpdate")
	@ResponseBody
	public Object showUpdate(String typeId){

		logger.info("进入显示...");

		//HttpServletResponse response = Tools.getResponse();

		TbType member = typeService.findById(typeId);

		//logger.info(gson.toJson(vo));

		return member;

	}
	
	@RequestMapping("/findAll")
	@ResponseBody
	public Object findAll(){
		logger.info("进入查询所有...");
		
		List<TbType> list = typeService.findAll();
		return list;
	}
}
